home *** CD-ROM | disk | FTP | other *** search
- Path: news.th-darmstadt.de!news
- From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
- Newsgroups: comp.lang.c++
- Subject: Re: Calling the wrong constructor
- Date: 14 Apr 1996 10:08:42 +0200
- Organization: Fachbereich Informatik, TH Darmstadt
- Sender: enno@kitz.inferenzsysteme.informatik.th-darmstadt.de
- Message-ID: <ltn34fns6s.fsf@kitz.inferenzsysteme.informatik.th-darmstadt.de>
- References: <4kot87$796@earth.njcc.com>
- NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
- In-reply-to: mike@pluto.njcc.com's message of 13 Apr 1996 18:53:59 GMT
- X-Newsreader: Gnus v5.1
-
-
- This might seem like a basic question, but lets say you something set u
- like the following:
-
- class Parent { Parent() { allocsomething} ~Parent() { deletesomething }... };
- class Foo : Parent { Foo() { allocsomething } ~Foo() {deletesomething }... };
- class Contain { Parent *ptr; ... };
-
- both parent, and foo allocate memory, and will free it up when being
- destructed. Contain just holds a pointer of type Parent, and its
- constructor takes such a pointer as a parameter.
- If I store a pointer of type Foo into contain.ptr and discard that
- pointer. Is there any way to have contain (via destructors) free up all
- memory allocated by Foo and/or Parent?
-
- Declare the dtor of 'Parent' as virtual.
- Otherwise deleting a subclass-object of 'Parent' through
- a pointer to 'Parent' will lead to undefined behavior.
-
- Enno
-
-